package com.xianniu.ltc.controller;

import cn.devezhao.commons.web.ServletUtils;
import cn.devezhao.commons.web.WebUtils;
import cn.devezhao.persist4j.engine.ID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rebuild.api.RespBody;
import com.rebuild.core.UserContextHolder;
import com.rebuild.core.configuration.general.DataListManager;
import com.rebuild.utils.JSONUtils;
import com.rebuild.web.BaseController;
import com.rebuild.web.user.signup.LoginController;
import com.xianniu.ltc.model.po.*;
import com.xianniu.ltc.model.vo.XnLeadTeamVO;
import com.xianniu.ltc.service.XnLeadTeamService;
import com.xxl.sso.core.entity.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author WangRuiJie
 * @Title: LeadTeamController
 * @Package com.xianniu.ltc.controller
 * @Description: 线索组管理controller
 * @date 2021/6/911:22 上午
 */
@RestController
@Slf4j
@RequestMapping(value = "/leadTeam")
public class XnLeadTeamController extends BaseController {

    @Autowired
    private XnLeadTeamService xnLeadTeamService;

    /**
     * 后台线索组-查询线索组列表
     * @return
     */
    @GetMapping("/selectLeadTeamForAdmin")
    public ModelAndView selectLeadForAdmin(HttpServletRequest request) {
        final ID user = getRequestUser(request);
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        ModelAndView mv = createModelAndView("/xianniu/admin/lead/lead-team");
        mv.getModel().put("entityName", "XnLeadTeam");
        mv.getModel().put("entityLabel", "线索池");
        mv.getModel().put("entityIcon", "entityIcon");
        JSON config = DataListManager.instance.getFieldsLayout("XnLeadTeam", user, tenantCode);
        mv.getModel().put("DataListConfig", JSON.toJSONString(config));
        return mv;
    }

    /**
     * 新前端调用返回JSON：后台线索组-查询线索组列表
     * @return
     */
    @GetMapping("/selectLeadTeamForAdmin-new")
    public JSONAware selectLeadForAdminNew(HttpServletRequest request) {
        final ID user = getRequestUser(request);
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("entityName", "XnLeadTeam");
        jsonObject.put("entityLabel", "线索池");
        jsonObject.put("entityIcon", "entityIcon");
        JSON config = DataListManager.instance.getFieldsLayout("XnLeadTeam", user, tenantCode);
        jsonObject.put("DataListConfig", JSON.toJSONString(config));
        return jsonObject;
    }

    /**
     * 下拉框-查询线索组列表
     * @param po
     * @return
     */
    @PostMapping("/leadTeamSelector")
    public JSONAware leadTeamSelector(@RequestBody QueryLeadTeamAdminPO po, HttpServletRequest request) {
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("后台线索组-后台查询线索组列表:{}", JSON.toJSON(po));
        IPage<XnLeadTeamVO> voiPage = xnLeadTeamService.selectLeadForAdmin(po);
        JSONObject json = new JSONObject();
        json.put("pageList", voiPage);
        return json;
    }

    /**
     * 后台线索组-新增线索组
     * @param po
     * @return
     */
    @PostMapping("/addLeadTeamForAdmin")
    public JSONAware addLeadTeamForAdmin(@RequestBody AddXnLeadTeamPO po , HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("后台线索组-新增线索组:{}", JSON.toJSON(po));
        if(Objects.isNull(po.getHeadBy()) || Objects.isNull(po.getUseDept())){
            return RespBody.errorl("负责人，使用部门不能为空");
        }
        ID currentUser = (ID) ServletUtils.getSessionAttribute(request, WebUtils.CURRENT_USER);
        po.setCreatedBy(currentUser.toString());
        ReturnT returnT = xnLeadTeamService.addLeadTeamForAdmin(po);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 后台线索组-转移分组
     * @param po
     * @return
     */
    @PostMapping("/transferLeadTeamForAdmin")
    public JSONAware transferLeadTeamForAdmin(@RequestBody TransferTeamPO po, HttpServletRequest request) {
        if (StringUtils.isEmpty(po.getFromLeadTeamId()) || StringUtils.isEmpty(po.getToLeadTeamId())){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("后台线索组-转移分组:{}", JSON.toJSON(po));
        ReturnT returnT = xnLeadTeamService.transferLeadTeamForAdmin(po);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }


    /**
     * 后台线索组-修改分组
     * @param po
     * @return
     */
    @PostMapping("/updateLeadTeamForAdmin")
    public JSONAware updateLeadTeamForAdmin(@RequestBody UpdateLeadTeamPO po, HttpServletRequest request) {
        if (StringUtils.isEmpty(po.getMetadata().getId())){
            return RespBody.errorl("PlsSet1FieldShowLeast");
        }
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        ID currentUser = (ID) ServletUtils.getSessionAttribute(request, WebUtils.CURRENT_USER);
        po.setModifiedBy(currentUser.toString());
        log.info("后台线索组-修改分组:{}", JSON.toJSON(po));
        ReturnT returnT = xnLeadTeamService.updateLeadTeamForAdmin(po);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 后台-根据ID删除线索组
     * @param id
     * @return
     */
    @GetMapping("/deleteLeadTeamForAdmin")
    public JSONAware deleteLeadTeamForAdmin(String id, HttpServletRequest request) {
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("后台-根据ID删除线索组:{}", JSON.toJSON(id));
        ReturnT returnT = xnLeadTeamService.deleteLeadTeamForAdmin(id);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

}
